import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;

/*
 * 	CalculatorGUI.java : This class gets cities from user and sends them to Clock class. 
 * 		This class has an instance of ClockGUI class. calculator.java
 *  @Author : BilCIS
 */
public class CalculatorGUI extends JPanel
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 867088855494825963L;
	
	//Variables declaration
	private static final Font font = new Font("monspaced", Font.PLAIN, 20);
	private JTextField displayField;
	private JButton clearButton;
	private JPanel buttonPanel;
	private JPanel opPanel;
	private	JPanel clearPanel;
	private	JPanel contentPane;
	
    private boolean   startNum = true; 
    private String    previousOprtion;
    private	String 	  buttonOrder;
    
    private Calculator calc = new Calculator(); 
    private Employee emp;
	// End of variables declaration
	
	public CalculatorGUI(Employee emp)
	{  
		this.emp = emp;
		previousOprtion  = "="; 
		buttonOrder = "789456123 0 ";  
		
		create();
		this.setVisible(true);
	 }
	  
	 public void create()
	 {
		 //
		 //	Create controls
		 //
		 displayField = new JTextField("0", 12);
		 clearButton = new JButton("Clear");
	     buttonPanel = new JPanel();
	     opPanel = new JPanel();
	     clearPanel = new JPanel();
	     contentPane = new JPanel();
		//
		//Register action listeners
		//
	    ActionListener opListener = new OpListener();
		ActionListener numListener = new NumListener();
	    clearButton.addActionListener(new ClearListener());
	    //
	    //Mnemonic
	    //
	    clearButton.setMnemonic('c');
		//
	    //Font
		//
		clearButton.setFont(font);
	    displayField.setFont(font);
	    //
	    //Locations
	    //
		displayField.setHorizontalAlignment(JTextField.RIGHT);
		clearPanel.setLayout(new FlowLayout());
		contentPane.setLayout(new BorderLayout(5, 5));
		contentPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		//
		//Add controls to pane
		//
		this.add(contentPane);
		clearPanel.add(clearButton);
	    contentPane.add(displayField, BorderLayout.NORTH );
	    contentPane.add(buttonPanel   , BorderLayout.CENTER);
	    contentPane.add(opPanel       , BorderLayout.EAST  );
	    contentPane.add(clearPanel    , BorderLayout.SOUTH );
		//
		//Mini Buttons
		//
		buttonPanel.setLayout(new GridLayout(5, 3, 2, 2));
	    for (int i = 0; i < buttonOrder.length(); i++) 
	     {
	    	 String keyTop = buttonOrder.substring(i, i+1);
	         JButton b = new JButton(keyTop);
	         if (keyTop.equals(" ")) 
	         {
	             b.setEnabled(false);
	         } 
	         else 
	         {
	             b.addActionListener(numListener);
	             b.setFont(font);
	         }
	         buttonPanel.add(b);
	     }
	        
	     //Sets operators of calculator
	      opPanel.setLayout(new GridLayout(5, 1, 2, 2));
	      String[] opOrder = {"+", "-", "*", "/", "="};
	      for (int i = 0; i < opOrder.length; i++) 
	      {
	          JButton b = new JButton(opOrder[i]);
	          b.addActionListener(opListener);
	          b.setFont(font);
	          opPanel.add(b);
	      }  
	      
	     contentPane.setBorder(BorderFactory.createTitledBorder("Calculator"));
	     contentPane.setBackground(emp.getColor());
	     clearPanel.setBackground(emp.getColor());
	     opPanel.setBackground(emp.getColor());
	     buttonPanel.setBackground(emp.getColor());
	     
		//
		//Settings for dimension
		//	
		this.setSize(new Dimension(450, 350));
	 }
	 
	    //To clear calculations which are made
	    private void actionClear() 
	    {
	        startNum = true;
	        displayField.setText("0");
	        previousOprtion  = "=";
	        calc.setTotal("0");
	    }

	    public class OpListener implements ActionListener 
	    {
	        public void actionPerformed(ActionEvent e) 
	        {	
	        	//if any calculation does not made by the user 
	        	//it says ERROR - No operator
	            if (startNum) 
	            {
	                actionClear();
	                displayField.setText("ERROR - No operator");
	            } 
	            else 
	            {
	                startNum = true;
	                try 
	                {
	                    String displayText = displayField.getText();
	                    
	                    if (previousOprtion.equals("=")) {
	                        calc.setTotal(displayText);
	                    } else if (previousOprtion.equals("+")) {
	                        calc.add(displayText);
	                    } else if (previousOprtion.equals("-")) {
	                        calc.subtract(displayText);
	                    } else if (previousOprtion.equals("*")) {
	                        calc.multiply(displayText);
	                    } else if (previousOprtion.equals("/")) {
	                        calc.divide(displayText);
	                    }
	                    displayField.setText("" + calc.getTotalString());
	                    
	                } 
	                catch (NumberFormatException ex) 
	                {
	                    actionClear();
	                    displayField.setText("Error");
	                }
	                previousOprtion = e.getActionCommand();
	            }
	        }
	    }
	    
	    

	    public class NumListener implements ActionListener 
	    {
	        public void actionPerformed(ActionEvent e) 
	        {   
	        	//do display digit from the text
	            String digit = e.getActionCommand();
	            if (startNum) 
	            {
	                displayField.setText(digit);
	                startNum = false;
	            } 
	            else 
	            {
	                displayField.setText(displayField.getText() + digit);
	            }
	        }
	    }
	    
	    
	    public class ClearListener implements ActionListener 
	    {
	        public void actionPerformed(ActionEvent e) 
	        {
	            actionClear();
	        }
	    }
}
